import * as Types from 'containers/ActionTypes';
import * as Helper from 'Helper';
import * as MyWorkService from 'services/MyWorkService';
import * as MyAction from '../MyAction';

export function getMyPublishedWorks(pageId = 0, limit = 20) {
  return (dispatch, getState) => {
    const state = getState();
    const accessToken = Helper.getMyToken(state);

    if (!accessToken) {
      return Promise.reject('Unauthenticated');
    }

    return MyWorkService.getMyPublishedWorks(accessToken, pageId, limit)
      .then((data) => {
        dispatch({
          type: Types.MY.PUBLISHED.UPDATE_MY_PUBLISHED_COUNT,
          totalCount: data.totalCount,
        });
        return data;
      });
  };
}

export function deleteWork(workId) {
  return MyAction.deleteWork(workId, 'PUBLIC');
}

export function unpublishWork(workId) {
  return MyAction.changeWorkStatus(workId, 'PRIVATE');
}
